package 多线程.单例模式.mySIngletonLazy;

//懒
public class SingletonLazy {
    volatile private SingletonLazy single = null;
    static Object lock = new Object();  //等价于类对象

    public SingletonLazy getInstance() {
        //提高效率, 减少锁竞争, 锁竞争最多只有一轮
        if (single == null) {
            //防止多个线程一起读, 又同时一起创建多个实例
            synchronized (lock) {
                if (single == null) {
                    single = new SingletonLazy();
                }
            }
        }
        return single;
    }

    //设为私有方法, 不允许创建多个实例
    private SingletonLazy() {}
}
